package com.flong.selenium;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class DemoSelenium {

	public static ChromeDriver driver;
	public static String url="http://www.newrank.cn/public/info/detail.html?account=";
	public static int count1=0;
	public static int count2=0;
	
    static {
    	//http://blog.csdn.net/aerchi/article/details/8055913
    	//设置浏览器启动环境变量
    	System.setProperty("webdriver.chrome.driver", "D:\\DevSoft\\chromedriver.exe");
    	driver = new ChromeDriver();
    	driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);//SECONDS是秒的意思
    	count1=0;
    	count2=0;
    }
    
    public static void getData(List<String> acounts){
    	
    	for(int j=0;j<acounts.size();j++){
    		
    		String u=url+acounts.get(j);
    	/*	try {
				//Thread.sleep(3000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}*/
    		
    		driver.get(u);
    		//driver.getCapabilities();
    		
    		WebElement sdayE = driver.findElementById("info_detail_article_top");//根据Id去获取Web的元素
        	WebElement lastestE = driver.findElementById("info_detail_article_lastest");//根据Id去获取Web的元素
        	
        	List<WebElement> daysli=sdayE.findElements(By.tagName("li"));//更加li标签去查询html的信息
        	List<WebElement> lastestli=lastestE.findElements(By.tagName("li"));//更加li标签去查询html的信息
        	for(int i=0;i<daysli.size();i++){
        		WebElement e=daysli.get(i);
        		WebElement titleE=e.findElement(By.className("ellipsis"));
        		WebElement contentE=e.findElement(By.className("article-text"));
        		WebElement dateE=e.findElement(By.className("info-detail-article-date"));
        		WebElement readcountE=e.findElement(By.className("read-count"));
        		WebElement linksE=e.findElement(By.className("links-count"));
        		
        		//By.cssSelector(".red_box");这句话就说根据css的class=red_box进行查询同等于By.className("red_box")
        		
        		String a=titleE.getAttribute("href");
        		System.out.println("*********************"+count1+"标题："+titleE.getText()+"--"+a);
        		System.out.println("*********************"+count1+"链接："+a);
        		System.out.println("*********************"+count1+"摘要："+contentE.getText());
        		System.out.println("*********************"+count1+"时间："+dateE.getText());
        		System.out.println("*********************"+count1+"阅读数："+readcountE.getText()+"  点赞数："+linksE.getText());
        		count1++;
        	}
        	for(int i=0;i<lastestli.size();i++){
        		WebElement e=lastestli.get(i);
        		WebElement titleE=e.findElement(By.className("ellipsis"));
        		WebElement contentE=e.findElement(By.className("article-text"));
        		WebElement dateE=e.findElement(By.className("info-detail-article-date"));
        		WebElement readcountE=e.findElement(By.className("read-count"));
        		WebElement linksE=e.findElement(By.className("links-count"));
        		
        		String a=titleE.getAttribute("href");
        		System.out.println("++++++++++"+count2+"标题："+titleE.getText()+"--"+a);
        		System.out.println("++++++++++"+count2+"链接："+a);
        		System.out.println("++++++++++"+count2+"摘要："+contentE.getText());
        		System.out.println("++++++++++"+count2+"时间："+dateE.getText());
        		System.out.println("++++++++++"+count2+"阅读数："+readcountE.getText()+"  点赞数："+linksE.getText());
        		count2++;
        	}
        	//driver.close();
        	//driver.quit();
    		
    	}
    }

    

    public static void main(String[] args) throws IOException, Exception {
    	List<String> li=new ArrayList<String>();
    	li.add("touchbox");
    	li.add("gzlife88");
    	li.add("gzsh88");
    	li.add("GZguangzhoulife");
    	li.add("huxiu_com");
    	li.add("dgdalaba");
    	li.add("hey0754");
    	li.add("zhcs333");
    	li.add("rmrbwx");
    	li.add("people_rmw");
    	getData(li);
    	//driver.navigate().refresh();
    	
    	driver.close();
    	driver.quit();
    	System.out.println("行数："+count1+"***"+count2);
    	count1=0;
    	count2=0;
    	
    }

}